package com.ewanghuiju.app.ui.home.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.home.SearchShopContract;
import com.ewanghuiju.app.model.bean.local.SearchHotBean;
import com.ewanghuiju.app.model.bean.response.HistorySearchResponBean;
import com.ewanghuiju.app.presenter.home.SearchShopPresenter;
import com.ewanghuiju.app.ui.home.adapter.SearchHotAdapter;
import com.ewanghuiju.app.util.AutoSoftUtils;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 搜索商品页面
 * @time 2020/2/17
 */
public class SearchShopActivity extends BaseActivity<SearchShopPresenter> implements SearchShopContract.View {
    @BindView(R.id.toolbar)
    LinearLayout mToolbar;

    @BindView(R.id.flowLayout)
    TagFlowLayout flowLayout;
    @BindView(R.id.et_search)
    EditText et_search;
    @BindView(R.id.hotRecyclerView)
    RecyclerView hotRecyclerView;
    @BindView(R.id.cv_history)
    CardView cvHistory;
    @BindView(R.id.cv_hotsearch)
    CardView cvHotsearch;

    private SearchHotAdapter searchHotAdapter;


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_searchshop;
    }

    @Override
    protected void initEventAndData() {
        String searchText = getIntent().getStringExtra(Constants.SEARCH_INPUT_BODY);
        if (!TextUtils.isEmpty(searchText) && et_search != null) {
            et_search.setText(searchText);
            AutoSoftUtils.showSoftInput(et_search);
            et_search.setSelection(searchText.length());
        }
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            /** * @param v * @param actionId 针对软键盘,若是实体键盘actionId=0 * @param event 针对实体键盘,若是软键盘event=null * @return 返回true表示自己处理Enter事件,当imeOptions="actionSearch"时返回false此方法会被调用两次 */
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || event.getKeyCode() == KeyEvent.ACTION_DOWN) {
                    String words = et_search.getText().toString().trim();
                    if (TextUtils.isEmpty(words)) {
                        StyleableToast.makeText(mContext, "请输入搜索内容", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return true;
                    }
                    AutoSoftUtils.hideInput(et_search);
//                    if (!TextUtils.isEmpty(words)) {
//                        if(xtabLayout.getSelectedTabPosition()==0){
//                            //淘宝
//                        }else{
                    //拼团
                    new StartActivityUtil(mContext, SearchShopResultActivity.class).putExtra(Constants.SEARCH_BODY,
                            words)
                            .startActivity(true);
//                        }
//                    }
                    return true;
                }
                return false;
            }
        });
//        xtabLayout.addTab(xtabLayout.newTab().setText("淘宝"));
//        xtabLayout.addTab(xtabLayout.newTab().setText("拼团"));
        LoadingDialogUtils.show(mContext);
        mPresenter.getHistorydata();
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String searchText = intent.getStringExtra(Constants.SEARCH_INPUT_BODY);
        if (!TextUtils.isEmpty(searchText) && et_search != null) {
            et_search.setText(searchText);
            AutoSoftUtils.showSoftInput(et_search);
            et_search.setSelection(searchText.length());
        }
    }


    /**
     * 热门搜索
     *
     * @param searchHotBeans
     */
    private void initHotList(List<SearchHotBean> searchHotBeans) {
        if (searchHotAdapter != null) {
            searchHotAdapter.setNewData(searchHotBeans);
            return;
        }
        searchHotAdapter = new SearchHotAdapter(R.layout.item_search_hot);
        searchHotAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SearchHotBean searchHotBean = (SearchHotBean) adapter.getItem(position);
                if (searchHotBean != null && !TextUtils.isEmpty(searchHotBean.getName())) {
                    new StartActivityUtil(mContext, SearchShopResultActivity.class).putExtra(Constants.SEARCH_BODY,
                            searchHotBean.getName())
                            .startActivity(true);
                }
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        hotRecyclerView.setLayoutManager(linearLayoutManager);
        hotRecyclerView.setAdapter(searchHotAdapter);
        hotRecyclerView.setHasFixedSize(true);
        hotRecyclerView.setNestedScrollingEnabled(false);
        searchHotAdapter.setNewData(searchHotBeans);
    }

    @OnClick({R.id.iv_back, R.id.iv_delete, R.id.tv_search})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.iv_back:
                AutoSoftUtils.hideInput(et_search);
                onBackPressedSupport();
                break;
            case R.id.iv_delete:
                AutoSoftUtils.hideInput(et_search);
                LoadingDialogUtils.show(mContext);
                mPresenter.deleteHistorydata("1", "");
                break;
            case R.id.tv_search:
                //搜索
                String searchText = et_search.getText().toString();
                if (TextUtils.isEmpty(searchText)) {
                    StyleableToast.makeText(mContext, "请输入搜索内容", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                new StartActivityUtil(mContext, SearchShopResultActivity.class).putExtra(Constants.SEARCH_BODY,
                        searchText)
                        .startActivity(true);
                break;
            default:
                break;
        }
    }


    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void showContent(String bean) {

    }

    @Override
    public void showMoreContent(int code, String message) {

    }

    @Override
    public void showtHistorydata(List<HistorySearchResponBean> list) {
        if (list != null && list.size() > 0) {
            cvHistory.setVisibility(View.VISIBLE);
            flowLayout.setAdapter(new TagAdapter(list) {
                @Override
                public View getView(FlowLayout parent, int position, Object o) {
                    TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_search_shop_history,
                            flowLayout, false);
                    HistorySearchResponBean historySearchResponBean = (HistorySearchResponBean) o;
                    tv.setText(historySearchResponBean.getName());
                    return tv;
                }
            });
            flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    String wordStr = list.get(position).getName();
                    if (!TextUtils.isEmpty(wordStr)) {
                        new StartActivityUtil(mContext, SearchShopResultActivity.class).putExtra(Constants.SEARCH_BODY,
                                wordStr)
                                .startActivity(true);
                    }
                    return true;
                }
            });
        } else {
            cvHistory.setVisibility(View.GONE);
        }
    }

    @Override
    public void deleteHistorySuccess() {
        //删除历史成功
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        cvHistory.setVisibility(View.GONE);
    }

    @Override
    public void showtHotWords(List<SearchHotBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (list.size() > 0) {
            cvHotsearch.setVisibility(View.VISIBLE);
        } else {
            cvHotsearch.setVisibility(View.GONE);
        }
        initHotList(list);
    }

    @Override
    public void deleteHistoryFail() {
        //删除历史失败

    }
}
